import { stringify } from 'qs';
import request from '../utils/request';

export async function addZubie(params) {
  return request('/gameZubie/', {
    method: 'POST',
    body: params,
  });
}

export async function modifyZubie(params) {
  return request(`/gameZubie/`, {
    method: 'PATCH',
    body: params,
  });
}

export async function closeGameProjectZubie(id) {
  return request(`/gameZubie/close/${id}`, {
    method: 'PATCH',
  });
}

export async function openProjectZubie(id) {
  return request(`/gameZubie/open/${id}`, {
    method: 'PATCH',
  });
}

export async function queryFenzuListForZubie(params) {
  const { zubieCode, phaseCode } = params;
  if (phaseCode) {
    return request(`/gameProjectZubieFenzu/zubie/${zubieCode}?phaseCode=${phaseCode}`, {
      method: 'GET',
    });
  } else {
    return request(`/gameProjectZubieFenzu/zubie/${zubieCode}`, {
      method: 'GET',
    });
  }
}

export async function queryZubieByCode(code) {
  return request('/gameZubie/code/' + code, {
    method: 'GET',
  });
}

export async function changeZubieStatus(payload) {
  const { zubieCode, status } = payload;
  return request(`/gameZubie/changeZubieStatus/${zubieCode}/${status}`, {
    method: 'POST',
  });
}

export async function beginChouqian(payload) {
  const { zubieCode } = payload;
  return request(`/gameZubie/beginChouqian/${zubieCode}`, {
    method: 'POST',
  });
}

export async function cancelChouqian(payload) {
  const { zubieCode } = payload;
  return request(`/gameZubie/cancelChouqian/${zubieCode}`, {
    method: 'POST',
  });
}

export async function cancelPhaseChouqian(payload) {
  const { zubieCode, phaseCode } = payload;
  return request(`/gameZubie/cancelPhaseChouqian/${phaseCode}`, {
    method: 'POST',
  });
}

export async function queryPosition(code) {
  return request('/gamePosition/getByZubieCode/' + code, {
    method: 'GET',
  });
}

export async function autoChouqian(params) {
  const { zubieCode, phaseCode } = params;
  var path = `/gameZubie/autoChouqian/${zubieCode}`;
  if (phaseCode && phaseCode !== null) {
    path = path + '?phaseCode=' + phaseCode;
  }
  return request(path, {
    method: 'POST',
  });
}

export async function queryChouqian(params) {
  console.log(params);
  const { zubieCode, phaseCode, headers } = params;
  if (phaseCode) {
    return request(`/gamePosition/getChouqianForZubie/${zubieCode}?phaseCode=${phaseCode}`, {
      headers: headers
        ? headers
        : {
            'x-page-num': 1,
            'x-page-size': 20,
          },
      method: 'GET',
    });
  } else {
    return request(`/gamePosition/getChouqianForZubie/${zubieCode}`, {
      headers: headers
        ? headers
        : {
            'x-page-num': 1,
            'x-page-size': 20,
          },
      method: 'GET',
    });
  }
}

export async function queryZhixu(params) {
  const { zubieCode, phaseCode, headers } = params;
  if (phaseCode) {
    return request(`/gameSchedule/getZhixuForZubie/${zubieCode}?phaseCode=${phaseCode}`, {
      headers: headers
        ? headers
        : {
            'x-page-num': 1,
            'x-page-size': 20,
          },
      method: 'GET',
    });
  } else {
    return request(`/gameSchedule/getZhixuForZubie/${zubieCode}`, {
      headers: headers
        ? headers
        : {
            'x-page-num': 1,
            'x-page-size': 20,
          },
      method: 'GET',
    });
  }
}

export async function queryNotSetPlaceGame(params) {
  const { gameId, headers } = params;

  return request(`/gameSchedule/queryNotSetPlaceGame/${gameId}`, {
    // headers: headers?headers:{'x-page-num': 1,
    //   'x-page-size': 20},
    method: 'GET',
  });
}

export async function queryScore(params) {
  const { zubieCode, phaseCode, headers } = params;
  if (phaseCode) {
    return request(`/gameScore/getScoreForZubie/${zubieCode}?phaseCode=${phaseCode}`, {
      headers: headers
        ? headers
        : {
            'x-page-num': 1,
            'x-page-size': 20,
          },
      method: 'GET',
    });
  } else {
    return request(`/gameScore/getScoreForZubie/${zubieCode}`, {
      headers: headers
        ? headers
        : {
            'x-page-num': 1,
            'x-page-size': 20,
          },
      method: 'GET',
    });
  }
}

export async function queryZhixuForChouqian(params) {
  const { zubieCode, phaseCode, curPage } = params;
  if (phaseCode) {
    return request(
      `/gameSchedule/queryZhixuForChouqian/${zubieCode}?phaseCode=${phaseCode}&curPage=${curPage}`,
      {
        method: 'GET',
      }
    );
  } else {
    return request(`/gameSchedule/queryZhixuForChouqian/${zubieCode}?curPage=${curPage}`, {
      method: 'GET',
    });
  }
}

export async function queryScoreData(params) {
  const { zubieCode, phaseCode, curPage } = params;
  if (phaseCode) {
    return request(
      `/gameSchedule/queryScoreData/${zubieCode}?phaseCode=${phaseCode}&curPage=${curPage}`,
      {
        method: 'GET',
      }
    );
  } else {
    return request(`/gameSchedule/queryScoreData/${zubieCode}?curPage=${curPage}`, {
      method: 'GET',
    });
  }
}

export async function queryAllZubieForGame(code) {
  return request('/gameZubie/getALLZubie/' + code, {
    method: 'GET',
  });
}

export async function modifyScore(payload) {
  return request(`/gameScore/saveOrUpdate`, {
    method: 'POST',
    body: payload,
  });
}

export async function queryPhase(code) {
  return request('/gamePhase/zubie/' + code, {
    method: 'GET',
  });
}

export async function queryFanganHeader(params) {
  const { zubieCode } = params;
  return request('/gameZubie/fangan/header/' + zubieCode, {
    method: 'GET',
  });
}

export async function queryFangan(params) {
  const { zubieCode } = params;
  return request('/gameZubie/fangan/data/' + zubieCode, {
    method: 'GET',
  });
}

export async function setKongchang(params) {
  const { id, placeCode, zubiePlaceCode } = params;
  return request(
    `/gameSchedule/createPlaceAndSet/${id}?placeCode=${placeCode}&zubiePlaceCode=${zubiePlaceCode}`,
    {
      method: 'PATCH',
    }
  );
}

export async function jiaohuanChangxu(params) {
  return request(`/gameSchedule/jiaohuanChangxu?${stringify(params)}`, {
    method: 'PATCH',
  });
}

export async function cancelSetPlace(code) {
  return request(`/gameSchedule/cancelZubiePlace/${code}`, {
    method: 'PATCH',
  });
}

export async function finishMatch(code) {
  return request(`/gameSchedule/finishMatch/${code}`, {
    method: 'PATCH',
  });
}

export async function modifyTeamInfo(params) {
  return request(`/gameUserGroup/updateTeam`, {
    method: 'POST',
    body: params,
  });
}

export async function deletePhase(params) {
  const { phaseId } = params;
  return request(`/gamePhase/${phaseId}`, {
    method: 'DELETE',
  });
}
